package com.ghh.app.photoframe;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class UpdateWidgetService extends Service {
	private final static String	TAG				= "System.out";
	public static PendingIntent	serviceIntent	= null;

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.i(TAG, "start service...");
		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
		ComponentName componentName = new ComponentName(getApplicationContext(), PhotoFrameWidgetProvider.class);
		int[] allWidgetIds = appWidgetManager.getAppWidgetIds(componentName);
		for (int appWidgetId : allWidgetIds) {
			Log.i(TAG, "update widget in service:" + appWidgetId);
//			PhotoFrameWidgetProvider.updateAppWidget(getApplicationContext(), appWidgetManager, appWidgetId);
		}
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	public static void startService(Context context) {
		AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		if (serviceIntent != null) {
			alarm.cancel(serviceIntent);
		}
		Intent intent = new Intent(context, UpdateWidgetService.class);
		serviceIntent = PendingIntent.getService(context, 0, intent, 0);
		alarm.setRepeating(AlarmManager.RTC, 60000, 60000, serviceIntent);
//		context.startService(intent);
	}

	public static void stopService(Context context) {
		AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		if (serviceIntent != null) {
			Log.i(TAG, "cancel service");
			alarm.cancel(serviceIntent);
		}
	}
	
//	public static void registerService(Context context) {
//		AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
//
//		// 如果serviceIntent不為null(已註冊過)
//		if (serviceIntent != null)
//			alarmManager.cancel(serviceIntent); // 清除
//
//		final int updateInterval = 60 * 60000;// 間隔時間單位為ms
//
//		// 間隔時間不為0
//		if (updateInterval != 0) {
//			// 如果serviceIntent為null
//
//			if (serviceIntent == null) {
//				// 建立intent
//				Intent intent = new Intent(context, UpdateWidgetService.class);
//				// 建立serviceIntent
//				serviceIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//			}
//			// 在AlarmManager設定重覆執行的Server的intent
//			alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + updateInterval, updateInterval,
//					serviceIntent);
//		}
//	}
//
//	private void setAlarm(Context context, int appWidgetId, int updateRateSeconds) {
//		Intent widgetUpdate = new Intent();
//		widgetUpdate.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
//		widgetUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] { appWidgetId });
//		// 给来自计时器的消息加标识
//		widgetUpdate.setData(Uri.withAppendedPath(Uri.parse(URI_SCHEME + "://widget/id/"), String.valueOf(appWidgetId)));
//		PendingIntent newPending = PendingIntent.getBroadcast(context, 0, widgetUpdate, PendingIntent.FLAG_UPDATE_CURRENT);
//		AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
//		if (updateRateSeconds >= 0) {
//			alarms.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), updateRateSeconds, newPending);
//		} else {
//			alarms.cancel(newPending);
//		}
//	}
}
